import { reactive } from "vue";
import {
  page as userDiseasePage,
  deleteByIds as userDiseaseDeleteByIds,
} from "@/api/userDisease";
import {
  page as userAllergyPage,
  deleteByIds as userAllergyDeleteByIds,
} from "@/api/userAllergy";
import router from "@/router/index";
import { ElMessage, ElMessageBox } from "element-plus";

export const _$ = reactive({
  state: {
    activeName: "disease",
  },
  query: {
    requestParam: {
      pageNum: 0,
      pageSize: 10,
      condition: "",
    },
    responseParam: {
      total: 0,
      records: [],
    },
  },
  delete: {
    requestParam: {
      ids: [],
    },
    responseParam: {},
  },
});

export const $ = {
  onSearch() {
    $.onTabClick(
      {
        paneName: _$.state.activeName,
      },
      null
    );
  },

  onDelete(id, type) {
    ElMessageBox.confirm("确定删除吗？", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      if (type == 1) {
        userDiseaseDeleteByIds({
          ids: id.toString(),
        }).then((_) => {
          $.onSearch();
          ElMessage({
            message: "删除成功",
            type: "success",
          });
        });
      } else {
        userAllergyDeleteByIds({
          ids: id.toString(),
        }).then((_) => {
          $.onSearch();
          ElMessage({
            message: "删除成功",
            type: "success",
          });
        });
      }
    });
  },

  onReset() {
    _$.query.requestParam = {
      pageNum: 0,
      pageSize: 10,
      condition: "",
    };
    $.onSearch();
  },

  handleSizeChange(val) {
    _$.query.requestParam.pageSize = val;
    $.onSearch();
  },

  handleCurrentChange(val) {
    _$.query.requestParam.pageNum = val;
    $.onSearch();
  },

  handleSelectionChange(e) {
    _$.delete.requestParam.ids.length = 0;
    e.forEach((item) => {
      _$.delete.requestParam.ids.push(item.id);
    });
  },

  onTabClick(pane, ev) {
    _$.query.responseParam.records.length = 0;
    _$.state.activeName = pane.paneName;
    if (pane.paneName === "disease") {
      userDiseasePage(_$.query.requestParam).then((data) => {
        _$.query.responseParam.total = data.total;
        _$.query.responseParam.records.push(...data.records);
      });
    } else if (pane.paneName === "allergy") {
      userAllergyPage(_$.query.requestParam).then((data) => {
        _$.query.responseParam.total = data.total;
        _$.query.responseParam.records.push(...data.records);
      });
    }
  },

  clean() {
    _$.query = {
      requestParam: {
        pageNum: 0,
        pageSize: 10,
        condition: "",
      },
      responseParam: {
        total: 0,
        records: [],
      },
    };
    _$.delete = {
      requestParam: {
        ids: [],
        delFlag: "N",
      },
      responseParam: {},
    };
  },
};

export const to = {
  goToIndex() {
    router.push({
      path: "/portal/health-data",
    });
  },

  goToDetail(id) {
    router.push({
      path: "/portal/health-data/detail",
      query: {
        id: id,
      },
    });
  },
};
